class Solution {
    public String reverseWords(String s) {
        String[] S = s.split(" ");

        int left = 0,right = S.length - 1;
        while(left < right){
            swap(left,right,S);
            left ++;
            right --;
        }
        StringBuilder ss = new StringBuilder(S[0]);
        for(int i = 1;i < S.length;i ++){
            if(S[i] != ""){
                ss.append(" ");
                ss.append(S[i]);
            }
        }
        return ss.toString();

    }
    public void swap(int i,int j,String[] S){
        String s = S[i];
        S[i] = S[j];
        S[j] = s;
    }
}